package com.st.qdmall.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.st.qdmall.dao.GoodsDao;
import com.st.qdmall.dao.OrderDao;
import com.st.qdmall.entity.Goods;
import com.st.qdmall.entity.Order;
import com.st.qdmall.service.IOrderService;

@Service
public class OrderServiceImpl implements IOrderService{

	@Autowired
	private GoodsDao goodsDao;
	@Autowired
	private OrderDao orderDao;
	
	
	@Override
	public int add(Order order) {
		// 订单生成： 
		// 1.  验证商品的库存
		Goods goods = goodsDao.selectById(order.getGoods_id());
		if (goods.getStore() < 1) {
			// 库存不足
			return -2;
		}		
		
		// 2.修改商品的库存
		goods.setStore(goods.getStore() - 1);
		int res = goodsDao.update(goods);
		
		if (res <= 0) {
			// 商品库存更新失败
			return -3;
		}
		
		// 3. 保存订单信息	
		order.setPrice(goods.getPrice());
		return orderDao.insert(order);
	}

}


